package org.itsolutions.javaBackup.gui.web;

import java.io.IOException;
import java.net.URL;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
import org.itsolutions.javaBackup.exceptions.AException;
import org.itsolutions.javaBackup.exceptions.GUIException;
import org.itsolutions.javaBackup.gui.web.properties.GUIWebProperty;
import org.itsolutions.javaBackup.logger.ELogLevel;
import org.itsolutions.javaBackup.logger.ILogger;
import org.itsolutions.javaBackup.logger.LoggerHelper;
import org.itsolutions.javaBackup.spi.gui.IGUIWebController;
import org.itsolutions.javaBackup.utils.FileUtils;
import org.itsolutions.javaBackup.utils.NetUtils;

import com.google.inject.Inject;
import com.google.inject.Injector;

/**
 * 
 * @author Tomas Fecko
 */
public class GUIWebController implements IGUIWebController {

    private static final ILogger 	LOGGER = LoggerHelper.getLogger(GUIWebController.class);
    private Injector 				mInjector;
	private Server 					mServer;

	/**
	 * Create web controller. This will find empty port starting at port 9000.
	 * @throws AException when empty port cannot be found
	 */
	@Inject GUIWebController(Injector pInjector) throws AException {
        mInjector = pInjector;
		// CHECKSTYLE:OFF
		try {
			mServer = new Server(NetUtils.selectPort(GUIWebProperty.WEB_SERVER_PORT.get(), 25)); // TODO add to properties
		} catch (IOException e) {
			LOGGER.log(ELogLevel.ERROR, e);
			throw new GUIException(e);
		}
		// CHECKSTYLE:ON
	}

	@Override
	public final void startWebServer() throws AException {
		try {
			URL resource = GUIWebController.class.getClassLoader().getResource("java-backup.war");
			String warLocation = FileUtils.copyFileFromClassPathToTemp(resource, "java-backup", "java-backup.war");

			WebAppContext context = new WebAppContext();
			context.setWar(warLocation);
			context.setContextPath("/");
			context.setParentLoaderPriority(true);
            context.addEventListener(mInjector.getInstance(ServletContextListener.class));

			mServer.setHandler(context);
			mServer.start();
		} catch (Exception e) {
			throw new GUIException(e);
		}
	}

	@Override
	public final void startWebServerInDevelopmentMode() throws AException {
		try {
			WebAppContext context = new WebAppContext();
			context.setDescriptor("../java-backup-gui-web/src/main/webapp/WEB-INF/web.xml");
			context.setResourceBase("../java-backup-gui-web/src/main/webapp/");
			context.setContextPath("/");
			context.setParentLoaderPriority(true);
            context.addEventListener(mInjector.getInstance(ServletContextListener.class));

			mServer.setHandler(context);
			mServer.start();
		} catch (Exception e) {
			throw new GUIException(e);
		}
	}

	@Override
	public final void stopWebServer() throws AException {
		if (mServer.isRunning() || mServer.isStarted()) {
			try {
				mServer.stop();
			} catch (Exception e) {
				throw new GUIException("Cannot stop embedded server", e);
			}
		} else {
			throw new GUIException("Embedded server is not running or it is starting at this time. Try later again.");
		}
	}
}





